package com.itlong.cloud.rabbitmq.config;

import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.utils.rabbitMQ.BaseConfig;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      用户服务消息队列配置。
 * </desc>
 *
 * @createDate 2017/08/29
 */
@Configuration
public class RabbitConfig extends BaseConfig {

    //声明与旺龙一卡通对接修改的数据操作队列，true表示持久化该队列
    @Bean
    public Queue dockWithSmartCardUsreRabbit() {
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_SMARTCARD_SAVE_MODIFY_QUEUE, true, false, false, map);
        return queue;
    }

    //声明交互器
    @Bean
    TopicExchange topicExchange() {
        return new TopicExchange(RabbitMQConstant.RABBITMQ_SMARTCARD_SAVE_EXCHANGE);
    }

    // 用户交互器
    @Bean
    TopicExchange topicUserExchange(){
        return new TopicExchange(RabbitMQConstant.RABBITMQ_USER_EXCHANGE);
    }


    // 停车场业务指令交互器
    @Bean
    TopicExchange topicParkingOrderExchange(){
        return new TopicExchange(RabbitMQConstant.RABBITMQ_PARKING_ORDER_EXCHANGE);
    }
    //绑定
    @Bean Binding dockWithSmartCardUserbinding() {
        return BindingBuilder.bind(dockWithSmartCardUsreRabbit()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_SMARTCARD_SAVE_MODIFY_ROUTING_KEY);
    }

    //*************************************保存用户登录日志和操作日志业务指令相关配置***********************************************

    // 保存用户登录日志业务指令处理队列
    @Bean
    public Queue userLoginLogSaveQueue(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_USER_LOGIN_LOG_SAVE_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定保存用户登录日志业务指令处理队列和路由key
    @Bean
    Binding loginLogSaveQueue(){
        return BindingBuilder.bind(userLoginLogSaveQueue()).to(topicUserExchange()).with(RabbitMQConstant.RABBITMQ_USER_LOGIN_LOG_SAVE_ROUTING_KEY);
    }

    // 保存SDK开发者访问日志业务指令处理队列
    @Bean
    public Queue sdkDeveloperLogSaveQueue(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_LOG_SAVE_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定保存SDK开发者访问日志业务指令处理队列和路由key
    @Bean
    Binding bindingSdkDeveloperLogSaveQueue(){
        return BindingBuilder.bind(sdkDeveloperLogSaveQueue()).to(topicUserExchange()).with(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_LOG_SAVE_ROUTING_KEY);
    }

    // 保存SDK开发者异常日志业务指令处理队列
    @Bean
    public Queue sdkDeveloperExceptionLogSaveQueue(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_EXCEPTION_LOG_SAVE_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定保存SDK开发者异常日志业务指令处理队列和路由key
    @Bean
    Binding bindingSdkDeveloperExceptionLogSaveQueue(){
        return BindingBuilder.bind(sdkDeveloperExceptionLogSaveQueue()).to(topicUserExchange()).with(RabbitMQConstant.RABBITMQ_SDK_DEVELOPER_EXCEPTION_LOG_SAVE_ROUTING_KEY);
    }

    // 保存用户操作日志业务指令处理队列
    @Bean
    public Queue userOperationLogSaveQueue(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_USER_OPERATION_LOG_SAVE_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定保存用户操作日志业务指令处理队列和路由key
    @Bean
    Binding operationLogSaveQueue(){
        return BindingBuilder.bind(userOperationLogSaveQueue()).to(topicUserExchange()).with(RabbitMQConstant.RABBITMQ_USER_OPERATION_LOG_SAVE_ROUTING_KEY);
    }

    // 保存用户操作日志业务指令处理队列
    @Bean
    public Queue robotLoginLogSaveQueue(){
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_ROBOT_LOGIN_LOG_SAVE_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定保存用户操作日志业务指令处理队列和路由key
    @Bean
    Binding bindingRobotLoginLogSaveQueue(){
        return BindingBuilder.bind(robotLoginLogSaveQueue()).to(topicUserExchange()).with(RabbitMQConstant.RABBITMQ_ROBOT_LOGIN_LOG_SAVE_ROUTING_KEY);
    }

    //停车场业务指令错误信息队列
    @Bean
    public Queue handleTimeTaskParkingErrorOrderSaveQueue() {
        Map<String, Object> map = new HashMap();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PARKING_ERROR_ORDER_QUEUE,true, false, false, map);
        return queue;
    }

    // 绑定停车场业务指令错误信息队列和路由key
    @Bean
    Binding bindingParkingErrorOrderSaveQueue(){
        return BindingBuilder.bind(handleTimeTaskParkingErrorOrderSaveQueue()).to(topicParkingOrderExchange()).with(RabbitMQConstant.RABBITMQ_PARKING_ERROR_ORDER_QUEUE_KEY);
    }

    //死信队列配置

    @Bean
    public Queue deadQueue(){
        Queue queue = new Queue("dead", true);
        return queue;
    }

    @Bean
    public DirectExchange deadLetterExchange() {
        return new DirectExchange(RabbitMQConstant.DEAD_LETTER_EXCHANGE);
    }

    @Bean
    public Binding deadLetterBindding(){
        return BindingBuilder.bind(deadQueue()).to(deadLetterExchange()).with(RabbitMQConstant.MAIL_QUEUE_FAIL);
    }

    public static void main(String[] args) {
    }
}
